﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using System.Linq.Expressions;
namespace BitPortal.Business
{
    public static partial class MediaBLL
    {
        public  static  int Delete(long mediaID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Media m = bitEnt.Medias.SingleOrDefault(a => a.MediaID == mediaID);
            if (m != null)
            {
                Statistic statistic = m.Statistic;
                DiscussionGroup discussionGroup = m.DiscussionGroup;
                bitEnt.Medias.DeleteObject(m);
                //Delete Statistic
                if (statistic != null)
                {
                    bitEnt.Statistics.DeleteObject(statistic);
                }
                //Delete DiscussionGroup
                if (discussionGroup != null)
                {
                    bitEnt.DiscussionGroups.DeleteObject(discussionGroup);
                }
                return bitEnt.SaveChanges();
            }
            return 0;
        }

        public static int Insert(this Media media)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            //Add Static
            Statistic statistic = new Statistic() { DatetimeLastModified = DateTime.Now, ViewUserIDs = "#", ViewUserIPs = "#", LikeUserIDs = "#", DislikeUserIDs = "#" };
            bitEnt.Statistics.AddObject(statistic);
            //Add Discusstion
            DiscussionGroup discussionGroup = new DiscussionGroup() { DatetimeCreated = DateTime.Now, LanguageCode = media.LanguageCode, DiscussionCount = 0 };
            bitEnt.DiscussionGroups.AddObject(discussionGroup);
            //Add Meida
            media.Statistic = statistic;
            media.DiscussionGroup = discussionGroup;
            bitEnt.Medias.AddObject(media);
            return bitEnt.SaveChanges();
        }

        public static IQueryable<Media> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = from ent in bitEnt.Medias
                       orderby ent.SortOrder descending, ent.MediaID descending
                       select ent;
            return list;
        }

        public static Media GetByID(long mediaID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.Medias.SingleOrDefault(ent => ent.MediaID == mediaID);
        }

        public static IQueryable<Media> GetByCondition(string languageCode = null, string title = null, string type = null, bool? isEnabled = null, int start = -1, int limit = -1, string sort = null, string dir = null)
        {
            var list = GetAll().Where(ent => (languageCode == null || ent.LanguageCode.Equals(languageCode)) &&
                    (title == null || ent.Title.Contains(title)) &&
                    (type == null || ent.Type == type) &&
                    (isEnabled == null || ent.IsEnabled == isEnabled)
                );

            //Start Sorting
            if (sort != null)
            {
                var param = Expression.Parameter(typeof(Media), "ent");
                var sortExpression = Expression.Lambda<Func<Media, object>>(Expression.Property(param, sort), param);
                if (dir == "DESC")
                {
                    list = list.OrderByDescending(sortExpression);
                }
                else
                {
                    list = list.OrderBy(sortExpression);
                }
            }
            //Start Paging
            if (start >= 0 && limit > 0)
            {
                list = list.Skip(start).Take(limit);
            }
            return list;
        }

        /// <summary>
        /// Lay ra danh sach media theo Gallery
        /// </summary>
        /// <param name="galleryID"></param>
        /// <param name="type"></param>
        /// <param name="isEnabled"></param>
        /// <returns></returns>
        public static IQueryable<Media> GetByGallery(int galleryID, string type = null, bool? isEnabled = null)
        {

            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var listMedia = from g in bitEnt.Galleries
                            from gm in bitEnt.MediasInGalleries
                            from m in bitEnt.Medias
                            where g.GalleryID == galleryID && g.GalleryID == gm.GalleryID && gm.MediaID == m.MediaID 
                            select m;
            return listMedia.Where(ent =>
                (type == null || ent.Type == type) &&
                (isEnabled == null || ent.IsEnabled == isEnabled)
                );

        }

        /// <summary>
        /// Lay ra danh sach media cua san pham
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="type"></param>
        /// <param name="isEnabled"></param>
        /// <returns></returns>
        public static IQueryable<Media> GetMediaInProduct(int productID, string type = null, bool? isEnabled = null)
        {

            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var listMedia = from p in bitEnt.Products
                            from pm in bitEnt.ProductImages
                            from m in bitEnt.Medias
                            where p.ProductID == pm.ProductID && pm.MediaID == m.MediaID
                            select m;
            return listMedia.Where(ent =>
                (type == null || ent.Type == type) &&
                (isEnabled == null || ent.IsEnabled == isEnabled)
                );

        }

    }
}